﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using NxPartyAffairsOpen.Models;
using System.Linq;


namespace   NxPartyAffairsOpen.Helpers
{
    public class InformationHelper
    {
        public static InformationModel GetInformation(NXDBDataContext db ,Guid InfoGuid)
        {
             Information info = db.Information.SingleOrDefault(l => l.InfoGuid == InfoGuid);

            InformationModel im = new InformationModel();
            if (info != null)
            {
                im.Author = info.Author;
                im.Content = info.Content;
                im.CreateTime = info.CreateTime;
                im.InfoGuid = info.InfoGuid;
                im.ShowTime = info.ShowTime ?? DateTime.MinValue ;
                im.SubTitle = info.SubTitle;
                im.Title = info.Title;
                im.GroupName = info.GroupName;

                 


            }
            //DataTable dt = Wicresoft.IPS.BizLogic.BLAttachment.GetAttachmentByInfoGuid(InfoGuid);

            //List<AttachmentModel> attchList = new List<AttachmentModel>();
            //foreach (DataRow item in dt.Rows)
            //{
            //    AttachmentModel am = EntityHelper.ConvertToEntity(item, typeof(AttachmentModel)) as AttachmentModel;
            //    am.Url = string.Format(Config.IPSAttachmentUrl, am.AttachmentID);
            //    attchList.Add(am);

            //}
            //im.AttachmentList = attchList;
            return im;
        }


        public static DataTable GetPagingList(string Fields, string Table, string Where, string Order, int PageIndex, int PageSize, bool UseDistinct, int Action, ref int TotalCount,Guid categoryID ,bool IsRecursive)
        {
            if (Where.Trim() == string.Empty)
                Where += " [Status]='AUDITED' and BackBoolean=1";
            else
            {
                Where += " and [Status]='AUDITED' and BackBoolean=1";
            }
            SqlParameter[] ps = new SqlParameter[11];
            SqlParameter p = new SqlParameter();
            p.DbType = DbType.String;
            p.ParameterName = "@Fields";
            p.Value = Fields;
            ps[0] = p;

            p = new SqlParameter();
            p.DbType = DbType.String;
            p.ParameterName = "@Table";
            p.Value = Table;
            ps[1] = p;

            p = new SqlParameter();
            p.DbType = DbType.String;
            p.ParameterName = "@Where";
            p.Value = Where;
            ps[2] = p;

            p = new SqlParameter();
            p.DbType = DbType.String;
            p.ParameterName = "@Order";
            p.Value = Order;
            ps[3] = p;

            p = new SqlParameter();
            p.DbType = DbType.Int32;
            p.ParameterName = "@PageIndex";
            p.Value = PageIndex + 1;
            ps[4] = p;

            p = new SqlParameter();
            p.DbType = DbType.Int32;
            p.ParameterName = "@PageSize";
            p.Value = PageSize;
            ps[5] = p;

            p = new SqlParameter();
            p.DbType = DbType.Boolean;
            p.ParameterName = "@UseDistinct";
            p.Value = UseDistinct;
            ps[6] = p;

            p = new SqlParameter();
            p.DbType = DbType.Int32;
            p.ParameterName = "@Action";
            p.Value = Action;
            ps[7] = p;


            p = new SqlParameter();
            p.DbType = DbType.Guid;
            p.ParameterName = "@CategoryID";
            p.Value = categoryID;
            ps[8] = p;

            p = new SqlParameter();
            p.DbType = DbType.Boolean;
            p.ParameterName = "@IsRecursive";
            p.Value = IsRecursive;
            ps[9] = p;

            p = new SqlParameter();
            p.DbType = DbType.Int32;
            p.ParameterName = "@TotalCount";
            p.Direction = ParameterDirection.Output;
            p.Value = TotalCount;
            ps[10] = p;


            SqlConnection con = new SqlConnection(Config.IPSConnectionKey);
            con.Open();
            SqlCommand sqlcom = new SqlCommand("Proc_U_GetInfoList", con);
            sqlcom.Parameters.AddRange(ps);
            sqlcom.CommandType = CommandType.StoredProcedure;
            //sqlcom.ExecuteReader();

            SqlDataAdapter adapter = new SqlDataAdapter(sqlcom); con.Close();
            DataSet ds = new DataSet();
            adapter.Fill(ds);

            //
            if(Action!=1)
            int.TryParse(ds.Tables[1].Rows[0][0].ToString(), out TotalCount);
            return ds.Tables[0];
        }

    }
}
